$(function () {
	//游戏名称
	var gameName = 'eat-chicken';
	//触摸点距离盒子的距离
	var box_x,box_y;
	//计时器,计数器
	var timerCounter,guostCounter;
	//全局计数变量
	var globalNum = 0;
	//吃多少鸡算成功
	var totalGuost = 1;
	//创建计时器
	var timer,moveTimer;
	//游戏区域的高度
	var gameAreaHeight = $('#gameArea').height();
	//人的尺寸
	var person_w = $('#person').width();
	var person_h = $('#person').height();
	//炸弹尺寸
	var tnt_w = $('.tnt').width();
	var tnt_h = $('.tnt').height();
	//火鸡尺寸
	var chicken_w = $('.chicken').width();
	var chicken_h = $('.chicken').height();

	//判断用户是否有抽奖(游戏)资格
	api.eligibleToPlay();
	//获取活动参数
	api.getGameConfig(gameName);
	//添加底部链接
	setFootLink('#startPage,#failPage,#successPage');

	//点击[开始捉鬼]按钮
	$('#startBtn').on('click',function () {
		if (limitNum <= 0) {
			Toast('抽奖次数不够');
			return;
		}
		$('#startPage').hide();
		$('#rulePage').show();
	});

	//点击[知道了]按钮
	$('#iKnowBtn').on('click',function () {
		$('#rulePage').hide();
		$('#gamePage').show();
		gameBegin();
	});

	//点击[再捉一次]按钮
	$('.tryAgain').on('click',function () {
		$('#sucessPage,#failPage,#lotterySuccessBox,#lotteryFailBox').hide();
		$('#startPage,#successBox').show();
	});

	//点击[抽奖]按钮
	$('#successLottery').on('click', function  () {
		if (limitNum <= 0) {
			Toast('抽奖次数不够');
			return;
		}
		limitNum--;
		$('#toast-content').text('抽奖中...');
		$('#toast-box').show();
		api.lottery(gameName, function(res){
			$('#successBox,#toast-box').hide();
			if (res.code == 200) {
				if (res.data.is_winning) {
					$('#prize-name').text(res.data.prize.name);
					$('#lotterySuccessBox').show();
				} else {
					$('#lotteryFailBox').show();
				}
			} else {
				$('#successPage').hide();
				$('#startPage').show();
				Toast(res.message);
			}
		})
	})

	//给人绑定拖动事件
	$('#gameArea').on('touchstart','#person',function(e){
		e.preventDefault();
		var elm = $(this).position();
		var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
		box_x = touch.pageX - elm.left;
	});
	$('#gameArea').on('touchmove','#person',function(e){
		e.preventDefault();
		var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
		$(this).css('left', touch.pageX - box_x + 'px');
	});

	//游戏开始
	function gameBegin() {
		//初始化
		timerCounter = 5;
		guostCounter = 0;
		globalNum = 0;
		$('#timeCounter').text(timerCounter);
		$('#counter').text(guostCounter);
		$('#gameArea').html('<div id="person"></div>');
		//倒计时
		timer = setInterval(function () {
			$('#timeCounter').text(--timerCounter);
			if (timerCounter === 0) {
				clearInterval(timer);
				clearInterval(moveTimer);
				$('#gamePage').hide();
				if (guostCounter >= totalGuost) {
					$('#successPage,#successBox').show();
					$('#lotterySuccessBox,#lotteryFailBox').hide();
					$('#sucessNum').text(guostCounter);
				} else {
					$('#failPage').show();
					$('#failNum').text(guostCounter);
					$('#differNum').text(totalGuost - guostCounter);
				};
			};
		}, 1000);
		//移动事件
		guostMove();
		moveTimer = setInterval(guostMove,1000/60);
	};


	//移动事件
	function guostMove() {
		//定时生成鸡或者炸弹
		if (globalNum%30 === 0) {
			createItem();
		};
		var guosts = $('.tnt,.chicken');
		var person_left = $('#person').position().left;
		var person_top = $('#person').position().top;
		guosts.map(function (ele, i) {
			var item_top = i.offsetTop;
			var item_left = i.offsetLeft;
			var item_w,item_h;
			//判断是鸡还是炸弹
			var isTnt = $(i).hasClass('tnt');
			if (isTnt) {
				item_w = tnt_w;
				item_h = tnt_h;
			} else {
				item_w = chicken_w;
				item_h = chicken_h;
			};
			//清除已经超出边界的
			if (item_top > gameAreaHeight) {
				$(this).remove();
				return;
			} else {
				i.style.top = item_top + 4 + 'px';
			};
			//碰撞检测
			if (item_left+item_w >= person_left && item_left <= person_left+person_w) {
				if (item_top+item_h >= person_top && item_top <= person_top+person_h) {
					$(this).remove();
					if (isTnt) {
						guostCounter--;
						if (guostCounter < 0) {
							guostCounter = 0;
						};
						$('#counter').text(guostCounter);
						$('#person').addClass("getBomb");
						setTimeout(function  () {
							$('#person').removeClass("getBomb");
						},300);
					} else {
						$('#counter').text(++guostCounter);
						$('#person').addClass("getChicken");
						setTimeout(function  () {
							$('#person').removeClass("getChicken");
						},300);
					};
				};
			};
		});
		globalNum++;
	};
	//随机生成鸡或者炸弹
	function createItem() {
		//父盒子宽度7.5rem 物品宽度1rem;
		//left值随机范围 0 - 6.5rem;
		//产生随机数
		var num = (Math.random()*6.5).toFixed(1);
		var itemClass = Math.random()>0.5?'tnt':'chicken';
		var html = '<div class="'+itemClass+'" style="top:0;left:'+num+'rem"></div>';
		$('#gameArea').append(html);
	};
});